from tool.read_file import ReadFile
import requests


def request_environment_info(environment: object = 'test') -> object:
    """
    :param environment:   测试环境 or 生产环境
    :return:   返回ip和headers信息
    """

    # 配置文件信息
    env_info = ReadFile.read_yaml('config/environment.yaml')
    if environment == 'test':
        # 测试环境对应的配置
        request_info = env_info['test_environment']
        return {'ip': request_info['http'] + request_info['domain_name'],
                'headers': request_info['headers']}
    elif environment == 'pro':
        # 生产环境对应的配置
        request_info = env_info['pro_environment']
        return {'ip': request_info['http'] + request_info['domain_name'],
                'headers': request_info['headers']}
    else:
        print('暂时只有测试和生产环境的配置')


def get_cookie():
    headers = {"Content-Type": "application/x-www-form-urlencoded"}
    data = {"username": "zsrzsrzsr", "password": "zsrzsrzsr"}
    res = requests.post(url="https://www.wanandroid.com/user/login", data=data, headers=headers)
    # 直接返回的格式 <RequestsCookieJar[<Cookie loginUserName_wanandroid_com=zsrzsrzsr for .wanandroid.com/>, <Cookie token_pass_wanandroid_com=4bf05ed0eafab7f14f2571bbd95d46f0 for .wanandroid.com/>, <Cookie JSESSIONID=217F699246F8D942BADA4B4F55F5837D for www.wanandroid.com/>, <Cookie loginUserName=zsrzsrzsr for www.wanandroid.com/>, <Cookie token_pass=4bf05ed0eafab7f14f2571bbd95d46f0 for www.wanandroid.com/>]>
    # 需要的格式 loginUserName=zsrzsrzsr;loginUserName_wanandroid_com=zsrzsrzsr;token_pass=4bf05ed0eafab7f14f2571bbd95d46f0;token_pass_wanandroid_com=4bf05ed0eafab7f14f2571bbd95d46f0;JSESSIONID=252B4B62F90758D1F5B751AA4EB3A255
    # 对cookie进行处理 ['<RequestsCookieJar[<Cookie', 'loginUserName_wanandroid_com=zsrzsrzsr', 'for', '.wanandroid.com/>,', '<Cookie', 'token_pass_wanandroid_com=4bf05ed0eafab7f14f2571bbd95d46f0', 'for', '.wanandroid.com/>,', '<Cookie', 'JSESSIONID=8E38280556A3E5B23C0182DFFAE52B78', 'for', 'www.wanandroid.com/>,', '<Cookie', 'loginUserName=zsrzsrzsr', 'for', 'www.wanandroid.com/>,', '<Cookie', 'token_pass=4bf05ed0eafab7f14f2571bbd95d46f0', 'for', 'www.wanandroid.com/>]>']
    cookies_split = str(res.cookies).split(' ')
    # 拼接指定字符串
    cookies = cookies_split[1] + ';' + cookies_split[5] + ';' + cookies_split[9] + ';' + cookies_split[13] + ';' + \
              cookies_split[17]
    return cookies


if __name__ == '__main__':
    print(get_cookie())
